﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SelfManagement.Utility.Extensions
{
    public static class EnumExtensions
    {

        /// <summary>
        /// html拓展 :用于获取枚举类型的中文描述
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static string EnumDescription(this Enum value)
        {
            Type enumType = value.GetType();
            string name = Enum.GetName(enumType, value);

            if (name != null)
            {
                FieldInfo fieldInfo = enumType.GetField(name);
                if (fieldInfo != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                        typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    if (attr != null)
                    {
                        name = attr.Description;
                    }
                }
            }
            return name;
        }
    }
}
